use Pod::Find qw(contains_pod);
use Pod::Html;
use File::Path;
use strict;

my $compiler = $ENV{HHC} || 'C:\\Program Files\\HTML Help Workshop\\hhc.exe';
-e $compiler or die "HTML Help compiler not found at '$compiler'.
If your compiler is located somewhere else, set the environment variable\
'HHC' to the proper location and try again.";

my $xs_folder = 'plugin/XS';
my $pm_folder = 'module';
my $doc_folder = 'doc';
# HTML is generated into a subfolder so it can be more easily zipped
# up to be distributed as plain HTML
my $html_subfolder = 'html';
my @pod_files;

# step 1: generate POD from XS files

# make sure the target POD folder exists
mkpath("$pm_folder/PlPN");

opendir XS, $xs_folder or die "Unable to read XS folder '$xs_folder': $!";
while (my $e = readdir XS) {
	next if -d "$xs_folder/$e";
	next unless contains_pod("$xs_folder/$e");	# first test: does it have POD?
	(my $podfile = $e)=~s/\.xs$/.pod/i;
	my $cmd = qq(perldoc -opod -T -d "$pm_folder/PlPN/$podfile" "$xs_folder/$e");
	next if system $cmd;	# second test: did the system call return 0?
	push @pod_files, "PlPN/$podfile";
}
closedir XS;

# step 2: convert POD from PM and POD files into HTML files

# make sure the target HTML folder exists
mkpath("$doc_folder/$html_subfolder/PlPN");

unshift @pod_files, 'PlPN.pm';

# external packages mentioned in the POD files
my @ext_pkg = qw(Win32::GUI);

for (@pod_files) {
	my $podfile = $_;
	
	(my $css = $_)=~s:[^/]+:..:g;
	$css=~s/\.\.$/PlPN.css/;
	
	s/\..+?$/.html/;
	pod2html(
		"$pm_folder/$podfile",
		"--css=$css",
		"--htmldir=$doc_folder/$html_subfolder",
		"--outfile=$doc_folder/$html_subfolder/$_",
		"--podroot=$pm_folder",
		"--podpath=.:PlPN",
		"--recurse",
		"--noverbose",
	);
	
	local $/ = undef;
	open HTML, "$doc_folder/$html_subfolder/$_" or die "Unable to open '$doc_folder/$html_subfolder/$_': $!";
	my $html = <HTML>;
	close HTML;

	# check for instances of </code><code> to pretty up a hack that is
	# unfortunately necessary to get around one of Pod::Html's brain-dead
	# assumption
	$html=~s:</code><code>::g;

	# get rid of links to external packages
	for (@ext_pkg) {
		$html=~s:<a href=".+?">$_</a>:<em>$_</em>:g;
	}
	
	open HTML, ">$doc_folder/$html_subfolder/$_" or die "Unable to write '$doc_folder/$html_subfolder/$_': $!";
	print HTML $html;
	close HTML;
}

# clean up pod2html's temp files
unlink('pod2htmd.tmp');
unlink('pod2htmi.tmp');

# step 3: create the chm file from manually created and automatically
#         generated HTML files

my @ordered = qw(
	PlPN PlPN::PN
	PlPN::Document PlPN::Options PlPN::SearchOptions PlPN::TextOutput
	PlPN::AppEventSink PlPN::DocumentEventSink PlPN::TextEditorEventSink
	PlPN::ScriptRegistry PlPN::ScriptRunner PlPN::Recorder
	PlPN::Menu
);

$#pod_files == $#ordered or
	die "File count mismatch: Ordered: $#ordered <=> Encountered: $#pod_files";

open HHP, ">$doc_folder/PlPN.hhp";
open HHC, ">$doc_folder/PlPN.hhc";
open DEV, ">$doc_folder/$html_subfolder/devdoc.html";

print HHP <<PROJECT;
[OPTIONS]
Title=PlPN: Help
Compatibility=1.1
Compiled file=PlPN.chm
Contents file=PlPN.hhc
Display compile progress=Yes
Full-text search=Yes
Language=0x409 English (United States)
Default topic=$html_subfolder/index.html
Default window=plpn
Binary TOC=Yes

[WINDOWS]
plpn="PlPN: Help","PlPN.hhc",,"$html_subfolder/index.html","$html_subfolder/index.html",,,,,0x63520,,0x603046,,,,,,,,0

[FILES]
$html_subfolder/PlPN.css
$html_subfolder/index.html
$html_subfolder/userdoc.html
$html_subfolder/devdoc.html
PROJECT

#
# hhc.exe (the help compiler) is very picky about formatting
# an <object> in an <li> must start on the same line as the <li>
# otherwise it will refuse to compile
#

print HHC <<CONTENTS;
<html>
<body>
<object type="text/site properties">
	<param name="ImageType" value="Folder">
</object>
<ul>
	<li><object type="text/sitemap">
			<param name="Name" value="User Documentation">
			<param name="Local" value="$html_subfolder/userdoc.html">
			<param name="ImageNumber" value="9">
		</object>
	</li>
	<li><object type="text/sitemap">
			<param name="Name" value="Developer Documentation">
			<param name="Local" value="$html_subfolder/devdoc.html">
			<param name="ImageNumber" value="2">
		</object>
	</li>
	<ul>
CONTENTS

print DEV <<TOP;
<html>
<head>
<title>Developer Documentation</title>
<link rel="stylesheet" href="PlPN.css" type="text/css" />
</head>
<body>
<h1>Developer Documentation</h1>
<p>The developer documentation consists of POD documentation converted to
HTML via <code>pod2html</code>.</p>
<ul class="undecorated">
TOP

for my $name (@ordered) {
	(my $file = $name)=~s!::!/!g;
	$file .= '.html';
	
	print HHP "$html_subfolder/$file\n";
	print HHC <<ITEM;
		<li><object type="text/sitemap">
				<param name="Name" value="$name">
				<param name="Local" value="$html_subfolder/$file">
				<param name="ImageNumber" value="9">
			</object>
		</li>
ITEM
	print DEV qq(<li><a href="$file">$name</a></li>\n);
}

print HHC <<END;
	</ul>
</ul>
</body>
</html>
END

print DEV <<END;
</ul>
</body>
</html>
END

close HHP;
close HHC;
close DEV;

(my $hhp = "$doc_folder\\PlPN.hhp")=~s:/:\\:g;
system qq("$compiler" "$hhp");
